<?php

/**
 * Created by PhpStorm.
 * User: zhuhe
 * Date: 17/5/9
 * Time: 上午9:29
 */
class api_weixin
{
    private $m;
    private $_pk;
    private $_apiTable;

    private $_mid;
    private $postObj;
    public $_formUSER;
    public $_toUSER;
    public $_CreateTime;
    public $_MsgId;

    public $_type;
    public $_event;
    public $_picUrl;
    public $_media_id;
    public $_Format;
    public $_ThumbMediaId;
    //公众账号基本信息
    private $_isSer = false;
    private $_isSure = false;

    private $_exec =  array();
    private $_url;

    function __construct(){
        global $_G;
        $this->_apiTable = 'wx_apiuser';
        if($this->check()){
            if($_G['g_echostr']) exit($_G['g_echostr']);
        }
        else exit('301');
        $this->_mid = $_G['g_mid'];
        $this->_url = $_G['HOST_URL'];
        $this->_text = false;

    }
    private function check(){
        global $_G;

        DB::SetMainDB(DB_MAIN);
        $r = DB::fetch_first('select uid,Token,type,sure from %t where mid=%s',array($this->_apiTable ,$_G['g_mid']));
        if(!$r) return false;
        $_G['g_uid'] = $r['uid'];
        $token = $r['Token'];
        $this->_isSer = $r['type']=='Ser'?true:false;
        $this->_isSure = $r['sure']?true:false;

        $timestamp = $_G['g_timestamp'];
        $nonce = $_G['g_nonce'];
        $signature = $_G['g_signature'];

        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr,SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );
        if($tmpStr == $signature) return true;
        else return false;
    }

    private function initXML(){
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        if($this->postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA)){
            $this->_type = strtolower((string)$this->postObj->MsgType);
            $this->_CreateTime = (string)$this->postObj->CreateTime;
            $this->_formUSER = (string)$this->postObj->FromUserName;
            $this->_toUSER = (string)$this->postObj->ToUserName;
            $this->_MsgId = (string)$this->postObj->MsgId;

            return true;
        }else return false;
    }
    private function chat(){

        DB::SetMainDB(DB_MAIN);
        $id = DB::insert('wx_chat', array('inputtime'=>TIME, 'mid'=>$this->_mid, 'formuser'=>$this->_formUSER, 'touser'=>$this->_toUSER ,'type'=>$this->_type , 'CreateTime'=>$this->_CreateTime,'MsgId'=>$this->_MsgId), true);
        switch($this->_type){
            case 'text':

                DB::SetMainDB(DB_MAIN);
                DB::insert('wx_chat_'.$this->_type, array('id'=>$id, 'content'=>(string)$this->postObj->Content));
                break;
            case 'event':

                DB::SetMainDB(DB_MAIN);
                DB::insert('wx_chat_'.$this->_type, array('id'=>$id, 'Event'=>(string)$this->postObj->Event,'EventKey'=>(string)$this->postObj->EventKey,'Ticket'=>(string)$this->postObj->Ticket));
                break;
        }
    }

    public function index(){
        //初始化推送过来的消息
        $this->initXML();
        //记录消息
        $this->chat();

        //根据消息类型执行相关函数
        $fun = $this->_type;
        if(method_exists($this,$fun)){
            $msg = $this->$fun();
        }else $msg = '正在升级，请稍候再试，感谢您的关注。';

        //回复消息
        echo $this->reply($msg);
    }
    /*
    事件处理
    包含 关注 取消关注 扫描二维码
    */
    private function event()
    {
        global $_G;

        $Event = strtolower((string)$this->postObj->Event);
        $msg = array();
        switch ($Event) {
            case 'subscribe':  //关注回复

                //TODO subuser 函数可能不存在
                subuser($this->_formUSER, $this->_mid);

                $this->_exec[] = 'getInfo';
                $ticket = (string)$this->postObj->Ticket;
                $istext = false;

                //获取关注回复设置
                DB::SetMainDB(DB_MAIN);
                $reply = DB::fetch_first('select * from %t where mid=%s',array('wx_reply', $this->_mid));

                if($reply){

                    switch($reply['method']){
                        case 'home'://回复首页
                        case 'news'://回复图文
                            DB::SetMainDB(DB_MAIN);
                            $info = DB::fetch_all('select * from %t where replyid=%d and mid=%s and display=1',array('wx_reply_info', $reply['id'], $this->_mid));
                            foreach($info as $v){
                                $msg[] = array('title'=>$v['title'],'content'=>$v['content'],'thumb'=>$v['thumb'],'url'=>$v['url']);
                            }
                            break;
                        case 'messages'://一段话
                            DB::SetMainDB(DB_MAIN);
                            $info = DB::fetch_first('select * from %t where replyid=%d and mid=%s and display=1',array('wx_reply_info', $reply['id'], $this->_mid));

                            if($info) $msg = $info['content'];
                            if($msg) $istext = true;

                            break;
                        case 'event'://事件
                            DB::SetMainDB(DB_MAIN);
                            $info = DB::fetch_first('select * from %t where replyid=%d and mid=%s and display=1',array('wx_reply_info', $reply['id'], $this->_mid));

                            if($info['content']){

                                DB::SetMainDB(DB_MAIN);
                                $event = DB::fetch_first('select * from %t where id=%d and mid=%s and display=1',array('wx_event', $info['content'], $this->_mid));
                                if($event){
                                    if($event['type']=='image'){
                                        DB::SetMainDB(DB_MAIN);
                                        $media_id = DB::result_first("select media_id from %t where display=1 and tablename='wx_event' and tableid=%d",array("wx_image",$event['id']));
                                        $msg = array('type'=>'image','media_id'=>$media_id);
                                        //break;
                                    }
                                    //判断是否是我说的话
                                    else if($event['type'] == 'text'){
                                        DB::SetMainDB(DB_MAIN);
                                        $bind = DB::fetch_first('select * from %t where tablename=%s and type=%s and tableid=%d and mid=%s and display=1',array('wx_bind', 'wx_event', 'text', $event['id'], $this->_mid));
                                        if($bind) $msg = $bind['content'];
                                        if($msg) $istext = true;
                                    }else{
                                        DB::SetMainDB(DB_MAIN);
                                        $bind = DB::fetch_all('select * from %t where tablename=%s and tableid=%d and mid=%s and display=1 order by turn desc',array('wx_bind', 'wx_event', $event['id'], $this->_mid));
                                        foreach($bind as $v){
                                            $msg[] = array('title'=>$v['title'],'content'=>$v['content'],'thumb'=>$v['thumb'],'url'=>$v['url']);
                                        }
                                    }
                                }
                            }
                            break;
                    }
                }
            case 'scan':  //二维码扫描

                //TODO upuser 这个函数可能不存在
                upuser($this->_formUSER, $this->_mid);

                if($this->postObj->Ticket){
                    DB::SetMainDB(DB_MAIN);
                    $titarr= DB::fetch_first('select `use`,`who` from %t where ticket=%s and mid=%s', array('wx_ticket',(string)$this->postObj->Ticket,$this->_mid));

                    if($titarr['use'] == 'transfer_customer_service_'.$this->_mid){
                        $msg = '已经将您的信息接入到在线客服,稍候马上回复。';
                        return array('type'=>'transfer_customer_service');
                    }
                    if(!is_array($this->_exec) || !in_array('getInfo',$this->_exec)){
                        $this->_exec[] = 'getInfo';
                    }


                    //扫描二维码的功能都写到这里
                    DB::SetMainDB(DB_MAIN);
                    DB::insert('wx_ticketed', array('ticket'=>(string)$this->postObj->Ticket, 'openid'=>$this->_formUSER, 'inputtime'=>TIME, 'mid'=>$this->_mid, 'use' => $titarr['use']));


                    $tit = $titarr['use'];

                    if($tit){
                        $sw = explode('_',$tit);
                        switch($sw[0]){
                            default:
                                //获取用途组合
                                $msgF = $this->getFuncEvent($sw[0],$sw[1].'|'.$this->postObj->Ticket);
                                break;
                        }
                        if(is_array($msgF)){
                            if($msgF['type']=='image'){
                                return $msgF;
                            }
                            return array('type'=>'news','list'=>$msgF);
                            unset($msgF);
                        }elseif(is_string($msgF)){
                            $msg = $msgF;
                        }
                    }
                    if(!$msg){
                        $msg = '您扫描了二维码';
                        $istext = true;
                    }
                }

                if(is_string($msg)){
                    if($msg) return array('type'=>'text','list'=>$msg);
                }elseif(is_array($msg)){
                    if($msg['type']=='image'){
                        return $msg;
                    }
                    return array('type'=>'news','list'=>$msg);
                }
                return 'success';
                break;
            case 'unsubscribe':

                //TODO unuser 函数可能不存在
                unuser($this->_formUSER, $this->_mid);
                return 'success';
                break;
            case 'location':
                return 'success';
                break;
            case 'click':
                $msg = array();
                $EventId = (string)$this->postObj->EventKey;
                //if($this->_mid == 'SPEGEKIGFN') $EventId = 247;
                $msg = $this->ExecEvent($EventId);
                if($this->_text){
                    if($msg['type'] == 'image'){
                        return $msg;
                    }
                    if(strlen($msg)) return array('type'=>'text','list'=>$msg);
                }else{
                    if($msg['type'] == 'image'){
                        return $msg;
                    }
                    if(count($msg)) return array('type'=>'news','list'=>$msg);
                }
                return 'success';
                break;
            case 'card_pass_check':
                return 'success';
                break;
            case 'card_not_pass_check':
                return 'success';
                break;
            case 'user_get_card':
                return "success";

                break;
            case 'user_del_card':
                return "success";
                return '用户删除卡券';
                break;
            case 'user_consume_card':
                return "success";
                return '卡券被核销';
                break;
            case 'user_view_card':
                return 'success';
                break;
            case 'user_enter_session_from_card':
                return 'success';
                break;
            default:
                return $Event;
                break;
        }
    }
    //文本消息
    private function text()
    {
        global $_G;
        $msg = array();
        //分词
        $Content = $this->postObj->Content;

        $newslike = '%'.$Content.'%';

        //读取内存中的数据
        $msg = memory('get','wx-key-'.md5($this->_mid.$Content));

        /*
            先进行图文消息关键词匹配
            如果匹配到图文信息就返回图文消息
            如果未匹配到图文消息 那么转入到客服机器人
        */
        if(!$msg || (TIME - $msg['time'] > 180)){

            $inid = array();
            //根据关键词查找的新闻信息
            DB::SetMainDB(DB_MAIN);
            $n = DB::fetch_all('select id from %t where mid=%s and (title like %s or keywords like %s) and isgrab=1 and status=1 and display=1 limit 10',array('wx_news',$this->_mid,$newslike,$newslike));
            foreach($n as $v_n){
                DB::SetMainDB(DB_MAIN);
                $r = DB::fetch_first('select `title`,`thumb`,`description` from %t where id=%d', array('wx_news',$v_n['id']));
                $news[] = array('title'=>$r['title'],'content'=>$r['description'],'thumb'=>$r['thumb'],'url'=>'http://news/'.$v_n['id']);
                $inid[] = $v_n['id'];
            }

            //存在回复的图文信息就创建内存
            if(count($news)){
                memory('set','wx-key-'.md5($this->_mid.$Content), array('type'=>'news','data'=>$news,'time'=>TIME));

                $sql['id'] = date('YmdHis').rand_str(6,true);
                $sql['_uid'] = $_G['_uid'];
                $sql['uid'] = $_G['uid'];
                $sql['mid'] = $this->_mid;
                $sql['type'] = 'news';
                $sql['aid'] = implode(',',$inid);
                $sql['keywords'] = (string)$Content;
                $sql['ident'] = 'wx-key-'.md5($this->_mid.$Content);

                DB::SetMainDB(DB_MAIN);
                DB::insert('wx_keymemory',$sql);
                unset($sql);
            }

        }elseif($msg['type'] == 'news'){

            $news = $msg['data'];

        }elseif($msg['type'] == 'event'){

            $r = $msg['data'];
        }

        if(count($news)){

            $msg = array();

            foreach($news as $v_n){
                $msg[] = array('title'=>$v_n['title'],'content'=>$v_n['description'],'thumb'=>$v_n['thumb'],'url'=>$v_n['url']);
            }

            return array('type'=>'news','list'=>$msg);
        }

        if(!$r || (TIME - $msg['time'] > 180)){

            //搜索机器人

            if(!$r){
                DB::SetMainDB(DB_MAIN);
                $r = DB::fetch_all('SELECT `id`,`event` FROM %t where q like %s and display = 1 and mid=%s', array('wx_robot',$Content,$this->_mid));
            }
            if(!$r){
                DB::SetMainDB(DB_MAIN);
                $r = DB::fetch_all('SELECT `id`,`event` FROM %t where donot=1 and display = 1 and mid=%s', array('wx_robot',$this->_mid));
            }

            if($r){
                memory('set','wx-key-'.md5($this->_mid.$Content), array('type'=>'event','data'=>$r,'time'=>TIME));
                $inid = array();
                foreach($r as $rs){
                    $inid[] = $rs['id'];
                }

                $sql['id'] = date('YmdHis').rand_str(6,true);
                $sql['_uid'] = $_G['_uid'];
                $sql['uid'] = $_G['uid'];
                $sql['mid'] = $this->_mid;
                $sql['type'] = 'event';
                $sql['aid'] = implode(',',$inid);
                $sql['keywords'] = (string)$Content;
                $sql['ident'] = 'wx-key-'.md5($this->_mid.$Content);

                DB::SetMainDB(DB_MAIN);
                DB::insert('wx_keymemory',$sql);
                unset($sql);
            }
        }

        $k = array_rand($r);
        $c = $r[$k];
        $msg = $this->ExecEvent($c['event']);

        if($msg){
            if(is_array($msg)){
                if($msg['type']=='image'){
                    return $msg;
                }
                return array('type'=>'news','list'=>$msg);
            }else
            {
                return array('type'=>'text','list'=>$msg);
            }
        }
        return 'success';
    }

    private function voice(){
        return 'success';
    }

    private function image(){
        return '我已经收到您发来的图片，感谢您的关注。';
    }

    private function reply($msg){
        if($msg == 'success') return 'success';


        $header = '<ToUserName><![CDATA['.$this->_formUSER.']]></ToUserName><FromUserName><![CDATA['.$this->_toUSER.']]></FromUserName><CreateTime>'.TIME.'</CreateTime>';
        if(!is_array($msg)){
            $msg = str_replace("\r\n","\n",$msg);
            $type = '<MsgType><![CDATA[text]]></MsgType>';
            $body = '<Content><![CDATA['.$msg.']]></Content>';
            return '<xml>'.$header.$type.$body.'</xml>';
        }else if($msg == 'success') return 'success';

        //根据消息类型组合XML
        $body = '';
        switch($msg['type']){
            case 'news':
                $type = '<MsgType><![CDATA[news]]></MsgType>';
                $i = 0;
                foreach($msg['list'] as $v){
                    $i++;
                    $body .= "<item><Title><![CDATA[".$v['title']."]]></Title><Description><![CDATA[".$v['content']."]]></Description><PicUrl><![CDATA[".$this->thumb($v['thumb'])."]]></PicUrl><Url><![CDATA[".$this->url($v['url'])."]]></Url></item>";
                }
                $body = "<ArticleCount>".$i."</ArticleCount><Articles>".$body."</Articles>";
                break;
            case 'transfer_customer_service':
                $type = '<MsgType><![CDATA[transfer_customer_service]]></MsgType>';
                if($msg['list']) $type .= '<TransInfo><KfAccount>'.$msg['list'].'</KfAccount></TransInfo>';
                break;
            case 'image':
                $type = '<MsgType><![CDATA[image]]></MsgType>';
                $body = '<Image><MediaId><![CDATA['.$msg['media_id'].']]></MediaId></Image>';
                break;
            default:
                $msg['list'] = str_replace("\r\n","\n",$msg['list']);
                $type = '<MsgType><![CDATA[text]]></MsgType>';
                $body = '<Content><![CDATA['.$msg['list'].']]></Content>';
                break;
        }
        if($i == 0 && $msg['type'] == 'news') return '';
        return '<xml>'.$header.$type.$body.'</xml>';
    }

    private function ExecEvent($id){
        $msg = array();
        $id = intval($id);
        if($id<=0) return $msg;
        //return $id;
        //取出事件类型

        DB::SetMainDB(DB_MAIN);
        $r = DB::fetch_first('select * from %t where id=%d and display=1',array('wx_event', $id));

        if($r['type'] == 'text'){
            $this->_text = true;

            DB::SetMainDB(DB_MAIN);
            $bind = DB::fetch_first('select * from %t where tablename=%s and type=%s and tableid=%d and display=1',array('wx_bind', 'wx_event', 'text', $id));

            $msg = $bind['content'];
        }else if($r['type'] == 'image'){
            DB::SetMainDB(DB_MAIN);
            $MediaId = DB::result_first("select media_id from %t where display=1 and tableid=%d and tablename='wx_event'",array("wx_image",$id));
            $msg = array("type"=>"image","media_id"=>$MediaId);
        }else{
            $this->_text = false;

            DB::SetMainDB(DB_MAIN);
            $event = DB::fetch_all('select b.title,b.content,b.thumb,b.url,b.type,b.eid from %t as e,%t as b where b.tableid=e.id and b.tablename=%s and e.display=1 and b.display=1 and e.id=%d order by b.turn desc',array('wx_event','wx_bind','wx_event',$id));

            foreach($event as $k => $e){
                if($k > 9) break;
                $msg[] = array('title'=>$e['title'],'content'=>$e['content'],'thumb'=>$e['thumb'],'url'=>$e['url'],'id'=>$id);
            }
        }
        return $msg;
    }


    //TODO getAPIUser 函数可能不存在
    private function getFuncEvent($s,$id){
        if(strpos($id,'|') != false){
            $sw = explode('|',$id);
            $id = $sw[0];
        }
        switch($s){
            //事件
            case 'event':
                $msg = $this->ExecEvent($id);
                break;
            //新版商品信息
            case 'eshop#goods':
                DB::SetMainDB(3);
                $r = DB::fetch_first('select id,title,description,thumb from %t where id=%d and display=1 and mid=%s',array('es_goods',$id,$this->_mid));
                if($r['id']){
                    $apiuser = getApiUser($this->_mid);
                    $msg[] = array('title'=>$r['title'],'content'=>$r['description'],'thumb'=>$r['thumb'],'url'=>'https://'.$apiuser['AppId'].SC_WEBWX_URL.'/app/eshop/show?id='.$r['id']);
                }
                break;
            //门店信息
            case 'eshop#outlet':
                DB::SetMainDB(3);
                $r = DB::fetch_first('select id,title,description,thumb from %t where id=%d and display=1 and mid=%s',array('es_outlet',$id,$this->_mid));
                if($r['id']){
                    $apiuser = getApiUser($this->_mid);
                    $msg[] = array('title'=>$r['title'],'content'=>$r['description'],'thumb'=>$r['thumb'],'url'=>'https://'.$apiuser['AppId'].SC_WEBWX_URL.'/app/eshop/outletDetails?id='.$r['id']);
                }
                break;
            //酒店信息
            case 'eshop#hotel':
                DB::SetMainDB(3);
                $r = DB::fetch_first('select id,title,description,thumb from %t where id=%d and display=1 and mid=%s',array('es_hotel',$id,$this->_mid));
                if($r['id']){
                    $apiuser = getApiUser($this->_mid);
                    $msg[] = array('title'=>$r['title'],'content'=>$r['description'],'thumb'=>$r['thumb'],'url'=>'https://'.$apiuser['AppId'].SC_WEBWX_URL.'/app/hotel/hotel_details/'.$r['id']);
                }
                break;
            //新版子店铺
            case 'eshop#store':
                DB::SetMainDB(3);
                $r = DB::fetch_first('select id,title,description,thumb from %t where id=%d and display=1 and mid=%s',array('es_store',$id,$this->_mid));
                if($r['id']){
                    $apiuser = getApiUser($this->_mid);
                    $msg[] = array('title'=>$r['title'],'content'=>$r['description'],'thumb'=>$r['thumb'],'url'=>'https://'.$apiuser['AppId'].SC_WEBWX_URL.'/app/eshop/store_index?id='.$r['id']);
                }
                break;
            //图文信息
            case 'news':
                DB::SetMainDB(DB_MAIN);
                $r = DB::fetch_first('select id,title,description_old,thumb from %t where id=%d and display=1 and mid=%s',array('wx_news',$id,$this->_mid));
                if($r['id']){
                    $apiuser = getApiUser($this->_mid);
                    $msg[] = array('title'=>$r['title'],'content'=>$r['description'],'thumb'=>$r['thumb'],'url'=>'https://'.$apiuser['AppId'].SC_WEBWX_URL.'/app/index/show/'.$r['id']);
                }
                break;
            //报名活动
            case 'eshop#signup':
                DB::SetMainDB(DB_MAIN);
                $r = DB::fetch_first('select id,title,intro,thumb from %t where id=%d and display=1 and mid=%s',array('xl_sign_up',$id,$this->_mid));
                if($r['id']){
                    $apiuser = getApiUser($this->_mid);
                    $msg[] = array('title'=>$r['title'],'content'=>$r['intro'],'thumb'=>$r['thumb'],'url'=>'https://'.$apiuser['AppId'].SC_WEBWX_URL.'/app/signup/signup_details?id='.$r['id']);
                }
                break;
            //注册
            case 'reg':
                $r['id']=1;
                $r['title']='会员中心/注册/绑定';
                $r['intro']='快速注册';
//                $r['thumb']='http://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTJ2dEsaUjXZxoFVRIuIboicQHGgiaEvRxYiaehaIgFrk90dxIddjZp9xLz3OBuib7az5tg2cibJzbDQUgg/0';
                DB::SetMainDB(DB_MAIN);
                $data = DB::fetch_first('select id,is_reg from %t where openid=%s and display=1 and mid=%s',array('common_member',$this->_formUSER,$this->_mid));
                if($r['id']){
                    $apiuser = getApiUser($this->_mid);
                    if($data['is_reg']){
                        $msg[] = array('title'=>$r['title'],'content'=>$r['intro'],'url'=>'https://'.$apiuser['AppId'].SC_WEBWX_URL.'/app/member/index/?sopenid=');
                    }else{
                        $msg[] = array('title'=>$r['title'],'content'=>$r['intro'],'url'=>'https://'.$apiuser['AppId'].SC_WEBWX_URL.'/app/eshop/register');
                    }
                }
                break;
//            case 'reg':
//                DB::SetMainDB(DB_MAIN);
//                $data = DB::fetch_first('select id,is_reg from %t where openid=%d and display=1 and mid=%s',array('common_member',$this->_formUSER,$this->_mid));
//                $r['id']=1;
//                $r['thumb']='http://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTJ2dEsaUjXZxoFVRIuIboicQHGgiaEvRxYiaehaIgFrk90dxIddjZp9xLz3OBuib7az5tg2cibJzbDQUgg/0';
//                if($data['is_reg']==1){
//                    $r['title']='会员中心';
//                    $r['intro']='会员中心';
//                    $url='app/member/index/?sopenid=';
//                }else{
//                    $r['title']='注册/绑定';
//                    $r['intro']='快速注册';
//                    $url='app/eshop/register';
//                }
//                if($r['id']){
//                    $apiuser = getApiUser($this->_mid);
//                    $msg[] = array('title'=>$r['title'],'content'=>$r['intro'],'thumb'=>$r['thumb'],'url'=>'http://'.$apiuser['AppId'].SC_WEBWX_URL.'/'.$url);
//                }
//                break;
        }
        return $msg;
    }

    private function thumb($url){
        if($url){
            $url = strpos($url,'http')===0?$url:$this->_url.$url;
            return $url;
        }else '';
    }

    private function url($url){
        //要被替换的地址
        $array1 = array(
            'http://index',
            'http://member',
            'http://albums?',
            'http://albums/',
            'http://album/',
            'http://list/',
            'http://news?',
            'http://news/',
            'http://sign/',
            'http://form/',
            'http://ggk/',
            'http://lottery/',
            'http://card/',
            'http://scan/',
            'http://reserve',
            'http://shop/index',
            'http://village/index',
            'http://village/informations/',
            'http://village/information/',
            'http://village/interactions/',
            'http://village/interaction/',
            'http://village/records/',
            'http://village/record/',
            'http://active/'
        );
        //被替换后的地址
        $array2 = array(
            $this->_url.'app'.$this->_mid.'/index',
            $this->_url.'app'.$this->_mid.'/member/index',
            $this->_url.'app'.$this->_mid.'/albums',
            $this->_url.'app'.$this->_mid.'/albums?id=',
            $this->_url.'app'.$this->_mid.'/album?id=',
            $this->_url.'app'.$this->_mid.'/list?id=',
            $this->_url.'app'.$this->_mid.'/news',
            $this->_url.'app'.$this->_mid.'/news?id=',
            $this->_url.'app'.$this->_mid.'/sign?id=',
            $this->_url.'app'.$this->_mid.'/form?id=',
            $this->_url.'app'.$this->_mid.'/ggk?id=',
            $this->_url.'app'.$this->_mid.'/lottery?id=',
            $this->_url.'app'.$this->_mid.'/card?id=',
            $this->_url.'app'.$this->_mid.'/scan?id=',
            $this->_url.'app'.$this->_mid.'/reserve',
            $this->_url.'app'.$this->_mid.'/shop/index',
            $this->_url.'app'.$this->_mid.'/village/index',
            $this->_url.'app'.$this->_mid.'/village/informations?id=',
            $this->_url.'app'.$this->_mid.'/village/information?id=',
            $this->_url.'app'.$this->_mid.'/village/interactions?id=',
            $this->_url.'app'.$this->_mid.'/village/interaction?id=',
            $this->_url.'app'.$this->_mid.'/village/records?id=',
            $this->_url.'app'.$this->_mid.'/village/record?id=',
            $this->_url.'app'.$this->_mid.'/active/?id='
        );
        //$url = str_replace($array1, $array2, $url);
        $url = aurl($url, $this->_mid);
        //$url = $this->_url."app".$this->_mid."/redirect?url=".urlencode($url)."&openid=".$this->_formUSER;
        $str = is_int(strpos($url,"?")) ? "&" : "?";
        $url = $url.$str."openid=".$this->_formUSER;
        return $url;
    }
}